
#include <stdio.h>
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"



#define EC12_PIN_A GPIO_NUM_1
#define EC12_PIN_B GPIO_NUM_2
#define EC12_PIN_C GPIO_NUM_3
static QueueHandle_t gpio_isr_queue = NULL;
int current_ec12d_value = 0;




void IRAM_ATTR gpio_isr_handler(void* arg) {
    static TickType_t last_interrupt_time = 0;
    TickType_t current_time = xTaskGetTickCountFromISR();
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    // 去抖：至少间隔2ms才处理
    if(current_time - last_interrupt_time > pdMS_TO_TICKS(5)) {
        last_interrupt_time = current_time;
        // 处理中断响应
        uint32_t gpio_num = (uint32_t) arg;
        xQueueSendFromISR(gpio_isr_queue, &gpio_num, &xHigherPriorityTaskWoken);
        
    }

    // 如果有更高优先级任务被唤醒，请求调度
    if(xHigherPriorityTaskWoken == pdTRUE) {
        portYIELD_FROM_ISR();
    }
}


void receive_int_task(){
    uint32_t io_num;
    static int last_a_state = 0;
    static int value = 0;

   
   
    while(1){
        if(xQueueReceive(gpio_isr_queue, &io_num, portMAX_DELAY)) {
            //printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
            if(io_num == EC12_PIN_C){
                printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
            }else{
                int current_a_state = gpio_get_level(io_num);
                if(last_a_state == 0 && current_a_state==1){// 上升沿
                    

                    int current_b_state = gpio_get_level(EC12_PIN_B);
                    if(current_b_state == 1){
                        value++;
                    }else{
                        value--;
                    }
                    printf("value===============:%d\r\n",value);

                }else if(last_a_state == 1 && current_a_state == 0){// 下降沿
                    //last_a_state = current_a_state;

                    int current_b_state = gpio_get_level(EC12_PIN_B);
                    if(current_b_state == 1){
                        value--;
                    }else{
                        value++;
                    }
                    printf("value===============:%d\r\n",value);
                }
                last_a_state = current_a_state;
                current_ec12d_value = value;
            }
        }
        
    }
}

void example_EC12D_test_main(){
    //创建一个队列来处理来自isr的gpio事件
    gpio_isr_queue = xQueueCreate(100, sizeof(uint32_t));


    gpio_config_t gpio_config_b = {
            .pin_bit_mask = BIT64(EC12_PIN_B),          //配置引脚
            .mode =GPIO_MODE_INPUT,                  //输入模式
            .pull_up_en = GPIO_PULLUP_DISABLE,        //使能上拉
            .pull_down_en = GPIO_PULLDOWN_DISABLE,    //不使能下拉
            .intr_type = GPIO_INTR_DISABLE            //下降沿触发
    };
    // gpio参数配置
    gpio_config(&gpio_config_b);

    // 配置终端应交
    gpio_config_t interrupt_gpio_config = {
            .pin_bit_mask = BIT64(EC12_PIN_A),          //配置引脚
            .mode =GPIO_MODE_INPUT,                  //输入模式
            .pull_up_en = GPIO_PULLUP_DISABLE,        //使能上拉
            .pull_down_en = GPIO_PULLDOWN_DISABLE,    //不使能下拉
            .intr_type = GPIO_INTR_POSEDGE|GPIO_INTR_NEGEDGE            //下降沿触发
    };
    // gpio参数配置
    gpio_config(&interrupt_gpio_config);
    interrupt_gpio_config.pin_bit_mask = BIT64(EC12_PIN_C);
    gpio_config(&interrupt_gpio_config);


    // 安装 GPIO 中断服务程序
   
    gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1);
    
    // 设置中断处理函数           
    gpio_isr_handler_add(EC12_PIN_A, gpio_isr_handler, (void*)EC12_PIN_A);
    
    gpio_isr_handler_add(EC12_PIN_C, gpio_isr_handler, (void*)EC12_PIN_C);
    // 开启中断
    gpio_intr_enable(EC12_PIN_A);
    gpio_intr_enable(EC12_PIN_C);

     // 启动串口接收线程
    xTaskCreate(receive_int_task, " PIN INTERUPT", 4096, NULL, 10, NULL);
   
}